﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TPressure
{
    public partial class EditProjectForm : BaseForm
    {
        #region 属性
        /// <summary>项目模型
        /// 
        /// </summary>
        private Project obj = new Project();

        /// <summary>试压模板模型
        /// 
        /// </summary>
        public TemplateItem templateObj;

        /// <summary>工程编号
        /// 
        /// </summary>
        public string project_number;

        #endregion

        #region 自定义方法
        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["project_number"] = ProjectNumbernput.Text;
            List<Project> list = SqlSugarHelper.Inquireable<Project>(where);
            int count = list.Count;
            if (count > 0)
            {
                AntdUI.Message.error(this, where["project_number"] + "工程编号已存在", Font);
                return true;
            }
            else
            {
                return false;
            }
        }
        
        /// <summary>刷新模板信息
        /// 
        /// </summary>
        public void freshTemplateInfo()
        { 
            DesignPressureInput.Text = templateObj.design_pressure.ToString();
            if (templateObj.pipe_position.Contains("站内/集输"))
            {
                TestTypeSelect.Text = "站内/集输";
            }
            else if (templateObj.pipe_position.Contains("站内"))
            {
                TestTypeSelect.Text = "站内";
            }
            else if (templateObj.pipe_position.Contains("集输"))
            {
                TestTypeSelect.Text = "集输";
            }
        }

        /// <summary>根据编号查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Project ProjectWithNumber(string number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["project_number"] = number;
            List<Project> list = SqlSugarHelper.Inquireable<Project>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckSelectedRowExist(string number)
        {
            obj = ProjectWithNumber(number);
            if (obj == null)
            {
                Alert.Error("工程记录不存在", this);
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>根据编号查询模板记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Template TemplateWithNumber(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["template_number"] = number;
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>Template->TemplateItem
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private TemplateItem TemplateItem(Template item)
        {
            string first = string.Format("压力:{0}--时长：{1}", item.first_stage_pressure.ToString(), item.first_stage_minute.ToString());
            string second = string.Format("压力:{0}--时长：{1}", item.second_stage_pressure.ToString(), item.second_stage_minute.ToString());
            string third = string.Format("压力:{0}--时长：{1}", item.third_stage_pressure.ToString(), item.third_stage_minute.ToString());
            string rigor = string.Format("压力:{0}--时长：{1}", item.rigor_stage_pressure.ToString(), item.rigor_stage_minute.ToString());
            string intensity = string.Format("压力:{0}--时长：{1}", item.intensity_stage_pressure.ToString(), item.intensity_stage_minute.ToString());
            string edit = "否";
            if (item.edit_enable == 1)
            {
                edit = "是";
            }
           return new TemplateItem(item.template_number, item.pipe_position, item.design_pressure,
                first, second, third, rigor, intensity, edit);
        }

        #endregion

        #region 系统方法
        public EditProjectForm()
        {
            InitializeComponent();
        }
        private void EditProjectForm_Load(object sender, EventArgs e)
        {
            if (project_number.Length > 0)
            {
                bool res = CheckSelectedRowExist(project_number);
                if (!res)
                {
                    this.Close();
                    Alert.Error("工程记录不存在", this.Owner);
                    return;
                }
                Template template = TemplateWithNumber(obj.template_id);
                if (template == null)
                {
                    this.Close();
                    Alert.Error("试压模板记录不存在", this.Owner);
                    return;
                }
                templateObj = TemplateItem(template);

                ProjectNumbernput.Text = obj.project_number;
                ProjectNameInput.Text = obj.project_name;
                StakeMarkInput.Text = obj.stake_mark;
                PipeSpecificationInput.Text = obj.pipe_specification;
                PipeLengthInput.Text = obj.pipe_length.ToString();
                MediumInput.Text = obj.medium;
                DesignPressureInput.Text = obj.design_pressure.ToString();
                PipeNatureSelect.Text = obj.pipe_nature;
                TestTypeSelect.Text = obj.test_type;
                CompanyInput.Text = obj.company;
                ManometerPrecisionSelect.Text = obj.manometer_precision;
                ManometerRangeSelect.Text = obj.manometer_range;
            }
        }
        #endregion

        #region 按钮点击
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (ProjectNumbernput.Text.Length == 0)
            {
                Alert.Error("请输入工程编号", this);
                ProjectNumbernput.Focus();
                return;
            }
            if (ProjectNameInput.Text.Length == 0)
            {
                Alert.Error("请输入工程名称", this);
                ProjectNameInput.Focus();
                return;
            }
            if (PipeLengthInput.Text.Length == 0)
            {
                Alert.Error("请输入管道长度", this);
                PipeLengthInput.Focus();
                return;
            }

            if (templateObj.template_number <= 0)
            {
                Alert.Error("请选择试压模板", this);
                return;
            }

            obj.project_number = ProjectNumbernput.Text;
            obj.project_name = ProjectNameInput.Text;
            obj.stake_mark = StakeMarkInput.Text;
            obj.pipe_specification = PipeSpecificationInput.Text;
            obj.pipe_length = float.Parse(PipeLengthInput.Text);
            obj.medium = MediumInput.Text;
            obj.design_pressure = float.Parse(DesignPressureInput.Text);
            obj.pipe_nature = PipeNatureSelect.Text;
            obj.test_type = TestTypeSelect.Text;
            obj.company = CompanyInput.Text;
            obj.manometer_precision = ManometerPrecisionSelect.Text;
            obj.manometer_range = ManometerRangeSelect.Text;
            obj.template_id = templateObj.template_number;

            List<Project> list = new List<Project>();
            list.Add(obj);
            int res = SqlSugarHelper.Updateable(list);
            if (res > 0)
            {
                ProjecManagetForm frm = (ProjecManagetForm)this.Owner;
                frm.LoadProjectList();
                this.Close();
                Alert.Info("编辑工程成功", this.Owner);
            }
        }

        private void DesignPressureInput_Click(object sender, EventArgs e)
        {
            SelectTemplateForm frm = new SelectTemplateForm();
            frm.type = 1;
            frm.Owner = this;
            frm.ShowDialog();
        }

        #endregion
    }
}
